﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using WarehouseManager.DataAccess;
using WarehouseManager.Model;

namespace WarehouseManager.View
{
    public partial class FormNhapHang : Form
    {
        int sum1 = 0;
        private string s;
        NhanvienAccess nhanvienAccess = new NhanvienAccess();
        NhacungcapAccess nhacungcapAccess = new NhacungcapAccess();
        PhieunhapAccess phieunhapAccess = new PhieunhapAccess();
        HanghoaAccess hanghoaAccess = new HanghoaAccess();
        CTPhieunhapAccess ctphieunhapAccess = new CTPhieunhapAccess();
        DataTable listhh = new DataTable();
       
        public FormNhapHang(string s)
        {
            this.s = s;
            if (s!=null)
            {
                
                InitializeComponent(0);
            }
            else
            {
                
                InitializeComponent(1);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FormNhanvien nv = new FormNhanvien();
            nv.Show();
        }


        private void FormNhapHang_Load(object sender, EventArgs e)
        {
            //txtGioitinhnv.Text = "sss";

            if (s == null)
            {
                loadNhanvien();
                layMaphieunhap();
                layNgaynhap();
                loadNhacungcap();
                loadMahang();
            }
            else
            {
                cbMancc.Enabled = true;
                cbManv.Enabled = true;
                dataGridView1.ReadOnly = true;
                loadAll();
                loadDataGridview();
            }
        }
        private void loadAll()
        {
            DataTable dt = new DataTable();
            dt = phieunhapAccess.layView(s);
            txtMaphieu.Text = dt.Rows[0]["maPN"].ToString();
            dtNgaylapphieu.Text = dt.Rows[0]["ngayNhap"].ToString();
            cbMancc.Text = dt.Rows[0]["maNCC"].ToString();
            txtHotenncc.Text = dt.Rows[0]["tenNCC"].ToString();
            txtSdtncc.Text = dt.Rows[0]["soDTncc"].ToString();
            txtDiachincc.Text = dt.Rows[0]["diaChincc"].ToString();
            cbManv.Text = dt.Rows[0]["maNV"].ToString();
            txtHotennv.Text = dt.Rows[0]["hoten"].ToString();
            txtGioitinhnv.Text = dt.Rows[0]["gioiTinh"].ToString();
            txtSdtnv.Text = dt.Rows[0]["soDTnv"].ToString();
        }
        private void loadDataGridview()
        {
            int t1, t2, sum = 0 ;
            DataTable dt = phieunhapAccess.layHanghoatheoMapn(s);
            dataGridView1.DataSource = dt;
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr=dt.Rows[i];
                t1 = int.Parse(dr["donGia"].ToString());
                t2= int.Parse(dr["soLuong"].ToString());
                sum += (t1 * t2);
                dataGridView1.Rows[i].Cells[0].Value = (t1 * t2).ToString();
            }
            label21.Text = sum.ToString() + " (vnđ)";
        }
        private void loadNhanvien()
        {

            listhh = nhanvienAccess.Laytoanbonhanvien();
            DataRow dataRow;
            for (int i = 0; i < listhh.Rows.Count; i++)
            {
                dataRow = listhh.Rows[i];
                this.cbManv.Items.Add(dataRow["maNV"].ToString());
            }
        }
        private void loadNhacungcap()
        {
            DataTable dt = new DataTable();
            dt = nhacungcapAccess.laytoanbonhacungcap();
            DataRow dataRow;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataRow = dt.Rows[i];
                cbMancc.Items.Add(dataRow["maNCC"].ToString());

            }
        }
        public void layMaphieunhap()
        {
            LayMa layma = new LayMa();
            DataTable dt = new DataTable();
            dt = phieunhapAccess.laytoanbophieunhap();
            DataRow dataRow = dt.Rows[(dt.Rows.Count - 1)];
            txtMaphieu.Text = layma.NextID(dataRow["maPN"].ToString());
        }
        public void layNgaynhap()
        {
            dtNgaylapphieu.Text = DateTime.Now.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                string s1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string s2 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                Phieunhap pn = new Phieunhap(txtMaphieu.Text, cbMancc.Text, DateTime.Parse(dtNgaylapphieu.Text), cbManv.Text);
                phieunhapAccess.themPhieunhap(pn);
                CTPhieunhap ctpn = new CTPhieunhap(txtMaphieu.Text, s1, int.Parse(s2));
                ctphieunhapAccess.themCTPhieunhap(ctpn);

            }
            MessageBox.Show("Bạn đã thêm thành công!");
        }
        private void cbMakhach_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = nhacungcapAccess.laytoanbonhacungcap();
            DataRow dataRow;
            dataRow = dt.Rows[cbMancc.SelectedIndex];
            txtHotenncc.Text = dataRow["tenNCC"].ToString();
            txtDiachincc.Text = dataRow["diaChi"].ToString();
            txtSdtncc.Text = dataRow["soDT"].ToString();
        }

        private void cbManv_SelectedIndexChanged(object sender, EventArgs e)
        {
            listhh = nhanvienAccess.Laytoanbonhanvien();
            DataRow dataRow;
            dataRow = listhh.Rows[cbManv.SelectedIndex];
            txtHotennv.Text = dataRow["hodemNV"].ToString() + " " + dataRow["tenNV"].ToString();
            txtGioitinhnv.Text = dataRow["gioiTinh"].ToString();
            txtSdtnv.Text = dataRow["soDT"].ToString();
        }
        public void loadMahang()
        {
            DataTable dataTable = new DataTable();
            dataTable = hanghoaAccess.layToanbohanghoa();
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow dr = dataTable.Rows[i];
                Column2.Items.Add(dr["maHH"].ToString());
            }
        }
        private void cbColumn2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = ((ComboBox)sender).SelectedIndex;
            DataTable dt = new DataTable();
            dt = hanghoaAccess.layToanbohanghoa();
            DataRow dataRow;
            dataRow = dt.Rows[selectedIndex];
            dataGridView1.CurrentRow.Cells[1].Value = dataRow["tenHH"].ToString();
            //dataGridView1.CurrentRow.Cells[2].Value = dataRow["soLuong"].ToString();
            dataGridView1.CurrentRow.Cells[3].Value = dataRow["donViTinh"].ToString();
            dataGridView1.CurrentRow.Cells[4].Value = dataRow["donGia"].ToString();
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                ComboBox column2 = e.Control as ComboBox;
                column2.SelectedIndexChanged += new EventHandler(cbColumn2_SelectedIndexChanged);
                Column4.ReadOnly = false;
            }

        }
        private void cbColumn4_TextChanged(object sender, EventArgs e)
        {
            
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
        }
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int t1, t2;

            if (dataGridView1.CurrentCell.ColumnIndex == 2)
            {
                t1 = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
                t2 = int.Parse(dataGridView1.CurrentRow.Cells[4].Value.ToString());
                sum1 += (t1 * t2)/2;
                dataGridView1.CurrentRow.Cells[5].Value = (t1 * t2).ToString();
                
            }
            label21.Text = sum1.ToString()+ " "+"(vnđ)";
            
        }
       
        
    }
}
